<?php
/***************************************************************************
*                                request.php
 *
 *   Begin date           	: 
 *   Copyright            	: 
 *   Email                	: 
 ***************************************************************************/
defined('PHP5_PHP') or die("Application is stopping!!!");
//checkLogin();
//get
$task 		= php5GetParam($_REQUEST, 'task', '');
$currPage = php5GetParam($_REQUEST, 'p', 1);
if(!$currPage) {
	$currPage = 1;
}
$smarty->setCompileDir(sprintf($php5RootTempalteCPath, ''));
$tpl = sprintf($php5TemplateFile, $language, 'newsletters', 'history.tpl');
if(!is_file($tpl )) {
	die("can not find template");
}
$api_url = 'https://api.smtp.com/v1/';
$key_smtp     = KEY_API_HUMMING; #your_key

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$senders_url = $api_url . '/account/senders.json?api_key=' . $key_smtp;
$res = smtpcom_get( $ch, $senders_url );
$senders = $res['senders'];
$summary = '';
$sender = '';
$time_start = date('01-m-Y', time());
$time_end = date('d-m-Y', time());
if(is_array($senders) && count($senders) > 0) {
	foreach($senders as $k => $arr){
		if($arr['login'] == php5GetConfig('config_smtp_username')) {
			$sender = $senders[$k];			
			break;
		}
	}
}
$msg = '';
//////////////////////////////
switch($task)
{
	case 'delete':
		$chk 		= php5GetParam($_REQUEST, 'cid', '');
		if (count($chk) > 0) {
			$query = "DELETE "
			. "\n FROM #__newsletter_mail "
			. "\n WHERE id IN (" . implode( ", ", $chk ) . ") AND  user_id = '".$php5Session->getVar("user_id")."'  ";
			$php5DB->setQuery( $query );
			if ($php5DB->query()) {
				$msg = 'Delete sucessfully!!!';
			}
			writeHistoryLogs(1, $php5Session->getVar("admin_id"), $query, $php5DB);
		}
		break;
}
///////////////////////////
if ($task == 'search') {
	$s_name = php5GetParam( $_REQUEST, 's_name', '' );
	$php5Session->setVar("5_s_name", $s_name);
} else {
	$s_name = $php5Session->getVar("5_s_name");
}

$s_name = $php5Session->getVar("5_s_name");

$limit = 10;
$where = array();
if ($s_name) {
	$where[] = " subject like '%" . mysql_real_escape_string($s_name) . "%' ";
}
$where[] = " nm.type = 'news' ";
$where[] = " nh.mailid = nm.mailid ";
$where[] = " user_id = '".$php5Session->getVar("user_id")."' ";
$query = "SELECT COUNT(*) "
. "\n FROM #__newsletter_mail "
. ( count( $where ) ? "\n WHERE " . implode( ' AND ', $where ) : "" )
;

$php5DB->setQuery( $query );
$recordTotal = $php5DB->loadResult();

$startLimit= ($currPage - 1) * $limit;

//list category
$query = "SELECT nh.* "
. "\n FROM #__newsletter_mail nm,  #__newsletter_history nh "
. ( count( $where ) ? "\n WHERE " . implode( ' AND ', $where ) : "" )
. "\n ORDER BY nh.date desc LIMIT $startLimit, $limit";
$php5DB->setQuery( $query );//echo $query;
$rows = $php5DB->loadObjectList();
//paging
$pageTotal = ceil($recordTotal / $limit);
$currPage = $currPage > $pageTotal ? $pageTotal : $currPage;
$paging = new php5Paging($pageTotal, $currPage);
$paging->url = sefBuild($php5WebPath, 'index.php?o=newsletters&m=main', 1)."?";
//assign
$categories = '';
$i = 1;
if($rows) {
	foreach($rows as $key => $row) {
		$categories[$key] = array();
		if($sender) {
			$date1 = '';
			$date2 = '';
			$time_start = date('d-m-Y', $row->date);
			$time_end = date('d-m-Y', $row->date);	
			if($time_start) {
				$date1 = "00:00%20{$time_start}";
			}
			if($time_end) {
				$date2 = "23:59%20{$time_end}";
			}	
			$summary_url = $api_url. '/account/senders/' . $sender['label'] . '/statistics/summary.json?api_key=' . $key_smtp . '&time_start="'.$date1.'"&time_end="'.$date2.'"';
	
			$res = smtpcom_get( $ch, $summary_url );
			$summary = $res['summary'];
			if(count($summary) > 0) {
				$categories[$key]['delivered_count'] = $summary['delivered_count'];
				$categories[$key]['bounced_count'] = $summary['bounced_count'];
			} else {
				$categories[$key]['delivered_count'] = 0;
				$categories[$key]['bounced_count'] = 0;
			}
		} else {
			$categories[$key]['received_count'] = 0;
			$categories[$key]['bounced_count'] = 0;
			$categories[$key]['delivered_count'] = 0;
		}
		$categories[$key]['idx'] = $i;
		$categories[$key]['id'] = $row->mailid;
		$categories[$key]['date'] = date('d/m/Y h:i', $row->date);
		$categories[$key]['ip'] = $row->ip;
		$categories[$key]['action'] = $row->action;
		$categories[$key]['link'] = sefBuild($php5WebPath, 'index.php?o=newsletters&m=edit&task=view&id='. $row->mailid, 1);
		$categories[$key]['preview'] = sefBuild($php5WebPath, 'index.php?o=newsletters&m=preview&task=view&id='. $row->mailid, 1);
		$categories[$key]['report'] = sefBuild($php5WebPath, 'index.php?o=reports&m=emails&task=view&id='. $row->mailid, 1)."?&time_start=".date("d-m-Y", $row->date)."&time_end=".date("d-m-Y", $row->date);
		if($i % 2 == 0) $categories[$key]['row'] = 1;
		else $categories[$key]['row'] = 0;
		
		$php5DB->setQuery("SELECT SUM(count) AS count1
					FROM #__rate_email				
					WHERE typeoftemp=".$row->mailid." AND type=2 ");
		$categories[$key]['open'] = intval($php5DB->loadResult());
		
		$php5DB->setQuery("SELECT SUM(count) AS count1
					FROM #__rate_email				
					WHERE typeoftemp=".$row->mailid." AND type=1 ");
		$categories[$key]['click'] = intval($php5DB->loadResult());
		$i++;
	}
}

$smarty->assign('categories', $categories);
$smarty->assign('s_name', $s_name);
$smarty->assign('msg', $msg);
$smarty->assign('total', count($categories));
$smarty->assign('paging', $paging->showDivNewsPages());
$smarty->assign('link_category', sefBuild($php5WebPath, 'index.php?o=newsletters', 1));
$smarty->assign('link_create', sefBuild($php5WebPath, 'index.php?o=newsletters&m=create', 1));
$smarty->assign('action', sefBuild($php5WebPath, 'index.php?o=newsletters&m=history&task=search', 1));
$smarty->assign('main', $smarty->fetch($tpl));
//$smarty->display($tpl);

return;

